#pragma once
#include <mutex>
#include  "../../type/ClientType.hpp"
#include "../../tool/ClassReflectionTool.hpp"
#include "../../config/interface/ClientConfigInterface.hpp"
#include "ExecutionInterface.hpp"




class ClientExecutionInterface : public ExecutionInterface,public ClassReflectionTool::Object<ClientType::EXECUTION_TYPE> {

protected:
     bool m_isRunning = false;

    //m_isRunning参数锁
    std::mutex m_isRunningMutex;

    //主执行器
    ClientExecutionInterface*  m_mainExecution;

    ClientConfigInterface* m_config;


private:
    /**
     *启动回调函数
     * @return bool true成功启动 flase启动失败
     */
    virtual bool startCallBackFunction(){}

    /**
     *关闭回调函数
     * @return bool true成功关闭 flase关闭失败
     */
    virtual bool closeCallBackFunction(){}





public:
    ClientExecutionInterface() =default;

    virtual ~ClientExecutionInterface()  = default;


     /**
      * 设置配置
      * @param config
      */
     void setConfig(ClientConfigInterface* config) {
        this->m_config = config;
    }

     /**
      * 设置主执行器
      * @param execution
      */
     void setMainExecution(ClientExecutionInterface* execution) {
        this->m_mainExecution = execution;
    }

     /**
      * 获取主执行器
      * @return
      */
     ClientExecutionInterface* getMainExecution() {
         return this->m_mainExecution;
     }



    /**
    * 启动执行器
    */
    void start() {
         if (this->m_isRunning)return;

         //上锁
         std::unique_lock<std::mutex> lock(this->m_isRunningMutex);

         //再次检查
         if (this->m_isRunning)return;

         //调用回调函数
         if (this->startCallBackFunction()) this->m_isRunning = true;

     }

    /**
     * 关闭执行器
     */
    void close() {

         if (!this->m_isRunning)return;

         std::unique_lock<std::mutex> lock(this->m_isRunningMutex);

         if (!this->m_isRunning)return;

         //调用回调函数
         if (this->closeCallBackFunction()) this->m_isRunning = false;
     }

};
